package cn.zc.tools.security.utils;

import cn.zc.student.entity.VO.StudentAndClassifyVO;
import cn.zc.tools.security.securityEntity.SecurityUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

    /**
     * 获取携带token用户的信息
     * @return
     */
    public SecurityUser getSecurityUser(){
        SecurityUser user = null;
        try {
            UsernamePasswordAuthenticationToken u = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            user = (SecurityUser) u.getDetails();
        }catch (Exception e){
            e.printStackTrace();
            user = new SecurityUser(null,null,"未登陆用户",null,null,false,false,false,false);
        }
        return user;
    }

    public boolean testedClassify(String classifyId){
        SecurityUser user = getSecurityUser();
        if(user.getStudentAndClassifyVO() != null){
            for (StudentAndClassifyVO classifyVO:user.getStudentAndClassifyVO()) {
                if(classifyVO.getCurriculumClassifyId().equals(classifyId)){
                    return true;
                }
            }
        }

        return false;
    }
}
